package com.sharecode.stock.strategy;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.sharecode.stock.init.StockInit;
import com.sharecode.stock.service.StockStrategyService;
import com.sharecode.stock.strategy.vo.StrategyVo;
import com.sharecode.stock.task.StockTask;
import com.sharecode.stock.utils.SpringUtil;
import com.sharecode.stock.vo.MailBean;
import com.sharecode.stock.vo.stock.StockHistory;
import com.sharecode.stock.vo.stock.StockStrategy;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class StrategySuper {
	public static boolean gt(BigDecimal a, BigDecimal b) {
		return a.doubleValue() > b.doubleValue();
	}

	public static boolean lt(BigDecimal a, BigDecimal b) {
		return a.doubleValue() < b.doubleValue();
	}

	public static BigDecimal max(BigDecimal a, BigDecimal b) {
		return a.doubleValue() > b.doubleValue() ? a : b;
	}

	public static BigDecimal min(BigDecimal a, BigDecimal b) {
		return a.doubleValue() < b.doubleValue() ? a : b;
	}

	/**
	 * 均线
	 * 
	 * @param klines
	 * @param numbers
	 * @return
	 */
	public static BigDecimal average(List<StockHistory> klines, int numbers) {
		BigDecimal average = new BigDecimal("0");

		if (CollectionUtils.isNotEmpty(klines) && klines.size() >= numbers) {
			int size = klines.size();
			int count = size - numbers;
			for (int i = size - 1; i >= count; i--) {
				average = average.add(klines.get(i).getClose());
			}
			average = average.divide(new BigDecimal(numbers), 2, BigDecimal.ROUND_HALF_EVEN);
		}

		return average;
	}

	/**
	 * 发送邮件通知
	 * 
	 * @param subject
	 * @param content
	 * @param vo
	 */
	public static void sendMail(StrategyVo vo, String msg) {
		if (StockInit.sendNotice) {
			StockTask.MAIL_BEANS.add(new MailBean("股票编码[" + vo.getCode() + "]", msg));
		}
	}

	public static boolean isDev() {
		return StringUtils.equals("dev", StockInit.active);
	}

	public static boolean isNotDev() {
		return !isDev();
	}

	public static void logInfo(StrategyVo vo, String msg) {
		List<StockHistory> klines = vo.getStockHistories();
		int i = klines.size() - 1;
		StockHistory kline = klines.get(i);
		StringBuffer info = new StringBuffer("[策略] ");
		info.append(kline.getDate()).append("|").append(vo.getCode()).append("|").append("|").append(msg);

		log.info(info.toString());
	}

	public static void logError(Exception e) {
		log.error(e.getMessage(), e);
	}

	protected static double getDouble(double d, Integer scale) {
		return new BigDecimal(d).setScale(scale, BigDecimal.ROUND_HALF_EVEN).doubleValue();
	}

	/**
	 * 统一处理
	 * 
	 * @param vo
	 * @param detail         放一些说明
	 * @param noticeCountMap
	 */
	protected static void strategy(StrategyVo vo, String detail, Map<String, Integer> noticeCountMap,int type) {
		setNoticeCountMap(vo, noticeCountMap);
		sendMail(vo, detail);
		logInfo(vo, detail);
		List<StockHistory> histories = vo.getStockHistories();
		String date = histories.get(histories.size() - 1).getDate();
		SpringUtil.getBean(StockStrategyService.class).save(new StockStrategy(vo.getCode(), date, detail,type,vo.getCycle()));
	}

	protected static void setNoticeCountMap(StrategyVo vo, Map<String, Integer> noticeCountMap) {
		noticeCountMap.put(vo.getEnergys().get(vo.getEnergys().size() - 1).getId(), 1);
	}

	protected static String getEnergyId(StrategyVo vo) {
		return vo.getEnergys().get(vo.getEnergys().size() - 1).getId();
	}

	/**
	 * 同一个周期，是否通知过
	 * 
	 * @param vo
	 * @param noticeCountMap
	 * @return
	 */
	protected static boolean checkNoticeCount(StrategyVo vo, Map<String, Integer> noticeCountMap) {
		return noticeCountMap.get(vo.getEnergys().get(vo.getEnergys().size() - 1).getId()) != null;
	}

}
